package com.colman.parser;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.colman.om.Coordinate;
import com.colman.om.Placemark;
import com.colman.om.Point;

public class MapManager {

	public static List<Placemark> getPlacemarks(final String file) {

		final List<Placemark> placemarkList = new ArrayList<Placemark>();

		final Document dom = XmlParser.getDom(new File(file));

		final NodeList nList = dom.getElementsByTagName("Placemark");

		for (int temp = 0; temp < nList.getLength(); temp++) {

			final Node nNode = nList.item(temp);

			final Placemark newPlacemark = new Placemark();

			if (nNode.getNodeType() == Node.ELEMENT_NODE) {

				final Element eElement = (Element) nNode;

				final Node item = eElement.getElementsByTagName("name").item(0);

				final String name = item.getTextContent();

				newPlacemark.setName(name);

				newPlacemark.setDescription(eElement
						.getElementsByTagName("description").item(0)
						.getTextContent());

				final Element pointNode = (Element) eElement
						.getElementsByTagName("Point").item(0);

				final String str = pointNode
						.getElementsByTagName("coordinates").item(0)
						.getTextContent();

				final String[] coordinates = str.split(",");

				final Coordinate coordinate = new Coordinate();
				
				coordinate.setLongitude(coordinates[0]);
				coordinate.setLatitude(coordinates[1]);
				coordinate.setAltitude(coordinates[2]);

				final Point newPoint = new Point();
				newPoint.setCoordinate(coordinate);

				newPlacemark.setPoint(newPoint);
			}

			placemarkList.add(newPlacemark);
		}

		return placemarkList;
	}

}
